﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Regex
{
    using System.Text.RegularExpressions;
    class Program
    {
        static void Main(string[] args)
        {
            var s1 = "dunso";
            var pattren = "dunso";
            Console.WriteLine(Regex.IsMatch(s1, pattren));

            RegexIsMatch();
            RegexMatch();
            RegexReplace();
            RegexSplit();
            Matches();
            Groups();

            Console.ReadLine();
        }

        private static void RegexIsMatch()
        {
            string[] values = { "111-22-3333", "111-2-3333" };
            string pattern = @"^\d{3}-\d{2}-\d{4}$";

            foreach (var value in values)
            {
                if (Regex.IsMatch(value, pattern))
                {
                    Console.WriteLine("{0} is valid", value);
                }
                else
                {
                    Console.WriteLine("{0} is not valid", value);
                }
            }
        }

        //获取字符串中一段的方式
        private static void RegexMatch()
        {
            var input = "This is dunso dunso";
            // \b 单词的边界
            // \w 表示单词
            // \W 此处用来匹配空格
            // \1 表示匹配成功的第一个Group
            // \b 表示单词的结束
            var pattern = @"\b(\w+)\W(\1)\b";
            Match match = Regex.Match(input, pattern);

            while(match.Success)
            {
                Console.WriteLine("Duplilcation {0} found", match.Groups[1].Value);
                match = match.NextMatch();
            }
        }

        private static void RegexReplace()
        {
            // \d 表示数字 
            // + 表示一个或者多个
            // \. 表示必须有一个点
            // {2} 重复两次

            string pattern = @"\b\d+\.\d{2}\b";
            // $$ 表示美元的符号
            // $& 表示原来匹配上的字符
            string replacement = "$$$&";
            string input = "Total cost : 103.64";
            Console.WriteLine(Regex.Replace(input, pattern, replacement)); 
     
        }

        private static void RegexSplit()
        {
            string input = "1. Egg 2. Bread 3. Mike 4. Coffee";
            // {1,2} 出现的次数在1和2之间
            string pattern = @"\b\d{1,2}\.\s";
            foreach (string item in Regex.Split(input, pattern))
            {
                if(!String.IsNullOrEmpty(item))
                {
                    Console.WriteLine(item);
                }
            }
        }
        
        private static void Matches()
        {
            MatchCollection matches;
            Regex r = new Regex("abc");
            matches = r.Matches("123abc4abcd");
            foreach (Match match in matches)
            {
                Console.WriteLine("{0} found at position {1}", match.Value, match.Index);
                Console.WriteLine("{0}", match.Result("$&, hello dunso!"));
            }

        }

        private static void Groups()
        {
            string input = "Born: July 28, 1989";
            string pattern = @"\b(\w+)\s(\d{1,2}),\s(\d{4})\b";
            Match match = Regex.Match(input, pattern);
            if(match.Success)
            {
                for(int ctr = 0; ctr < match.Groups.Count; ctr ++)
                {
                    Console.WriteLine("Group {0}: {1}", ctr, match.Groups[ctr].Value);
                }
            }
        }
    }
}
